#include <vector>
#include "CAlliance.h"
#include "CParty.h"
#include "CChar.h"
#include "CUpdatePartyPacket.h"
#include "define.h"

//Sends the 0xdd (party update) packet for 1 specific member of the party.
//	charYou is the callers character, this is needed to compare the zone to see if they are on the same map as you
//	charParty is the party member that the update is generating
//	inPartyNumber is the party number (in the event an alliance is set) if no alliance this can be 0
//	memberNumber is the players number in a party (0 apperently means remove)
CUpdatePartyPacket::CUpdatePartyPacket(CChar * charYou,CChar * charParty,unsigned int inPartyNumber,unsigned int memberNumber) {
	unsigned char partyFlags = 0x00;
	
	memset(this->data, 0, 0x18*2);
	this->type = 0xDD;
	this->size = 0x18;
	
	if ( (charParty->getParty() != NULL) && (charParty->inParty > 0) ) {
		//Put the party number in the partyFlag
		if (inPartyNumber > 0) 
			partyFlags = inPartyNumber-1;

		//Check to see if the character is the party leader
		if (charParty->getParty()->getLeader() == charParty)
			partyFlags += PARTY_LEADER;

		//Check to see if the alliance member is the alliance leader
		if ( (charYou->getParty()->inAlliance > 0) && (charYou->getParty()->getAlliance() != NULL) )
			if (charYou->getParty()->getAlliance()->getMainParty()->getLeader() == charParty)
				partyFlags += ALLIANCE_LEADER;
		
		// bleh, hack to remove QM if you leave a party
		if (memberNumber != 0) { 
			//Check to see if the alliance member is a quarter master
			if (charParty->getParty()->getQM() == charParty)
				partyFlags += PARTY_QM;
		}
	} else {
		//If your not in a party; you have no flags
		partyFlags = 0;
	}
	//charid of the player
	memcpy(&this->data[0x04-0x04],&charParty->m_id, sizeof(unsigned int));

	//the party flags calculated above
	this->data[0x14-0x04] = partyFlags;
		
	//Zone of the player
	//Get prevzone if player is in Moghouse
	if (charYou->getDisplayZone() != charParty->getDisplayZone()) {
		this->data[0x1F-0x04] = charParty->getDisplayZone();
	} else {
		unsigned int HP = charParty->getHp();
		unsigned int MP = charParty->getMp();
		unsigned short TP = charParty->h.tp;
		unsigned short targID = charParty->getTargId();
		memcpy(&this->data[0x08-0x04], &HP,sizeof(unsigned int));
		memcpy(&this->data[0x0C-0x04], &MP,sizeof(unsigned int));
		memcpy(&this->data[0x10-0x04], &TP,sizeof(unsigned short));
		memcpy(&this->data[0x18-0x04], &targID, sizeof(unsigned short));
		this->data[0x1A-0x04] = memberNumber;
		this->data[0x1D-0x04] = (unsigned char) (((float)charParty->getHp() / (float)charParty->getMaxHp()) * 100);
		this->data[0x1E-0x04] = (unsigned char) (((float)charParty->getMp() / (float)charParty->getMaxMp()) * 100);
	}

	//Set the name of the player
	memcpy(&this->data[0x20-0x04], charParty->m_name, 16);
	
}

CUpdatePartyPacket::~CUpdatePartyPacket() {

}
